import React, { useState, useEffect } from 'react';
import { userService } from '../services/userService';

const UserProfile = () => {
    const [user, setUser] = useState(null);
    const [loading, setLoading] = useState(true);
    const [error, setError] = useState(null);

    useEffect(() => {
        loadUserProfile();
    }, []);

    const loadUserProfile = async () => {
        try {
            const userData = await userService.getCurrentUser();
            setUser(userData);
        } catch (err) {
            setError('加载用户资料失败');
        } finally {
            setLoading(false);
        }
    };

    const handleUpdate = async (e) => {
        e.preventDefault();
        try {
            const updatedUser = await userService.updateProfile(user);
            setUser(updatedUser);
            alert('资料更新成功');
        } catch (err) {
            setError('更新资料失败');
        }
    };

    if (loading) return <div>加载中...</div>;
    if (error) return <div>{error}</div>;
    if (!user) return <div>未找到用户资料</div>;

    return (
        <div className="user-profile">
            <h2>个人资料</h2>
            <form onSubmit={handleUpdate}>
                <div>
                    <label>用户名：</label>
                    <input
                        type="text"
                        value={user.username}
                        disabled
                    />
                </div>
                <div>
                    <label>权限：</label>
                    <input
                        type="text"
                        value={user.permission}
                        disabled
                    />
                </div>
                <div>
                    <label>昵称：</label>
                    <input
                        type="text"
                        value={user.nickname || ''}
                        onChange={(e) => setUser({ ...user, nickname: e.target.value })}
                    />
                </div>
                <div>
                    <label>头像：</label>
                    <input
                        type="text"
                        value={user.avatar || ''}
                        onChange={(e) => setUser({ ...user, avatar: e.target.value })}
                    />
                </div>
                <div>
                    <label>手机号：</label>
                    <input
                        type="text"
                        value={user.phone || ''}
                        onChange={(e) => setUser({ ...user, phone: e.target.value })}
                    />
                </div>
                <div>
                    <label>邮箱：</label>
                    <input
                        type="email"
                        value={user.email || ''}
                        onChange={(e) => setUser({ ...user, email: e.target.value })}
                    />
                </div>
                <button type="submit">保存修改</button>
            </form>
        </div>
    );
};

export default UserProfile; 